<?php

if ("only" == "for netbeans completion") {
    //@see http://blogs.oracle.com/netbeansphp/entry/recent_improvements_in_phpunit_support    
    require_once 'PHPUnit/Framework.php';
}

/**
 * Tests for database integrity - does everybody has same database structure
 * 
 * @author Aurelijus Banelis
 */
class DbTest extends PHPUnit_Framework_TestCase {
    private static $settingsFile = '../../settings.php';
    
    public function testSettingsFile() {
        $this->assertTrue(file_exists(self::$settingsFile), "No settings file in project directory");
        $settings = include self::$settingsFile;
        $this->assertArrayHasKey('DB_HOST', $settings);
        $this->assertArrayHasKey('DB_USER', $settings);
        $this->assertArrayHasKey('DB_PASSWORD', $settings);
        $this->assertArrayHasKey('DB_DATABASE', $settings);
    }
    
    public function testConnection() {
        $this->connect();
        $this->disconect();
    }
    
    private function connect() {
        $settings = include self::$settingsFile;
        $connected = @mysql_connect($settings['DB_HOST'], $settings['DB_USER'], $settings['DB_PASSWORD']) !== false;
        $this->assertTrue($connected, 'Bad database connection settings: ' . print_r($settings, true));
        $db_connected = @mysql_select_db($settings['DB_DATABASE']);
        $this->assertTrue($db_connected, 'Bad database name: ' . print_r($settings, true));        
    }
    
    private function disconect() {
        @mysql_close();
    }
    
    public function testTables() {
        $this->connect();
        
        /* Directions */
        $sql = 'SELECT id, title FROM directions';
        $query = @mysql_query($sql) != false;
        $this->assertTrue($query, "Bad DIRECTIONS table structure: $sql");
        
        /* Historical quanties */
        $sql = 'SELECT `date`, measure_code, administrated FROM historical_quantities';
        $query = @mysql_query($sql) != false;
        $this->assertTrue($query, "Bad HISTORICAL_QUANTITIES table structure: $sql");
        
        /* Information systems */
        $sql = 'SELECT code, title FROM information_systems';
        $query = @mysql_query($sql) != false;
        $this->assertTrue($query, "Bad INFORMATION_SYSTEMS table structure: $sql");
        
        /* IS Subdivisions */
        $sql = 'SELECT is_code, subdivision_code, `date` FROM is_subdivisions';
        $query = @mysql_query($sql) != false;
        $this->assertTrue($query, "Bad IS_SUBDIVISIONS table structure: $sql");
        
        /* Subdivisions */
        $sql = 'SELECT code, title FROM subdivisions';
        $query = @mysql_query($sql) != false;
        $this->assertTrue($query, "Bad SUBDIVISIONS table structure: $sql");
        
        /* Support administration */
        $sql = 'SELECT measure_code, subdivision_code, `time`, `date` FROM support_administration';
        $query = @mysql_query($sql) != false;
        $this->assertTrue($query, "Bad SUPPORT_ADMINISTRATION table structure: $sql");
        
        /* Support measures */
        $sql = 'SELECT code, title, direction_id FROM support';
        $query = @mysql_query($sql) != false;
        $this->assertTrue($query, "Bad SUPPORT table structure: $sql");
        
        $this->disconect();
    }
    
    public function testTables2() {
        $this->connect();
        
        /* Forecast quantities */
        $sql = 'SELECT `date`, measure_code, received FROM forecast_quantities';
        $query = @mysql_query($sql) != false;
        $this->assertTrue($query, "Bad FORECAST QUANTITIES table structure: $sql");
        
        $this->disconect();
    }
}

